package com.lysj.merchant.management.controller;

import cn.hutool.crypto.digest.BCrypt;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.lysj.admin.utils.constant.CommonConstant;
import com.lysj.admin.utils.spring.foundation.base.BaseContent;
import com.lysj.admin.utils.web.ParamUtil;
import com.lysj.admin.utils.web.Resp;
import com.lysj.admin.utils.web.SelectItem;
import com.lysj.merchant.management.AuthConstant;
import com.lysj.merchant.management.domain.Permission;
import com.lysj.merchant.management.domain.User;
import com.lysj.merchant.management.service.PermissionService;
import com.lysj.merchant.management.service.UserService;
import com.lysj.merchant.merchant.service.StoreService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Created by zk on 2019-01-02 17:53
 * @description
 */
@RestController
@RequestMapping("/merchant")
public class ManagementController extends BaseContent {
    @Resource
    private UserService userService;

    @Resource
    private StoreService storeService;
    @Resource
    private PermissionService permissionService;


    @Value("${secret.key}")
    private String key;
    @Value("${secret.tokenExpiration}")
    private int tokenExpiration;

    @PostMapping("/login")
    public Resp login(String username, String password) {
        User user = userService.findByUsername(username);
        if (ParamUtil.isBlank(user)) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "该用户名不存在");
        }
        if (user.getDelFlag().equals(CommonConstant.DEL_FLAG)) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "该用户已被禁用");
        }
        if (!BCrypt.checkpw(password, user.getPassword())) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "密码错误");
        }
//        List<Permission> list = permissionService.findByUserId(user.getId());
//        if (list.size() == 0) {
//            return new Resp().error(Resp.Status.PARAM_ERROR, "权限不足,请联系商户分配");
//        }
        String token = JWT.create().withIssuer(AuthConstant.AUTH_NAME).withSubject(user.getId())
                .withExpiresAt(new Date(new Date().getTime() + tokenExpiration * 60 * 1000))
                .sign(Algorithm.HMAC512(key));
        return new Resp().success(token, "登录成功");
    }


    /*
     * @author drj
     * @date 2019-03-20 16:32
     * @Description :商户电脑打印端登录
     */
    @PostMapping("/common/login")
    public Resp printerLogin(String username, String password) {
        User user = userService.findByUsername(username);
        if (ParamUtil.isBlank(user)) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "该用户名不存在");
        }
        if (user.getDelFlag().equals(CommonConstant.DEL_FLAG)) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "该用户已被禁用");
        }
        if (!BCrypt.checkpw(password, user.getPassword())) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "密码错误");
        }
        List<SelectItem> selectItems;
        if (!user.getUserType().equals(1)) {
            selectItems = storeService.getRepository().findStore(user.getId());
        } else {
            selectItems = storeService.getRepository().findMerchantStore(user.getId());
        }
        String token = JWT.create().withIssuer(AuthConstant.AUTH_NAME).withSubject(user.getId())
                .withExpiresAt(new Date(new Date().getTime() + tokenExpiration * 60 * 1000))
                .sign(Algorithm.HMAC512(key));
        Map<String,Object> map = new HashMap<>();
        map.put("storeList",selectItems);
        map.put("token",token);
        return new Resp().success(map);
    }

}
